layui.use(['form', 'layer', 'element'], function () {
    var form = layui.form,
        $ = layui.jquery,
        element = layui.element;

    $(".role-name").click(function () {
        var roleId = $(this).parent().attr("data-id");
        $(".role-item").removeClass("active");
        $(this).parent().addClass("active");
        if ($("#perInfo ul li.layui-this").index() == 0) {
            xtreePer(roleId);
        }
    });

    $(".role-item .edit").click(function () {
        var roleId = $(this).parents("li").attr("data-id");
        layui.layer.open({
            title: "编辑角色",
            type: 2,
            area: ["350px", "245px"],
            content: "/role/edit/" + roleId
        });
        return false;
    });

    $(".role-item .del").click(function () {
        var roleId = $(this).parents("li").attr("data-id");
        layer.confirm('确定删除该角色？', {icon: 3, title: '提示信息'}, function (index) {
            $.post("/role/del/" + roleId, {}, function (res) {
                if (res.code == 0) {
                    layer.msg("删除成功！");
                    location.reload();
                } else {
                    layer.msg(res.msg);
                }
            });
        });
        return false;
    });

    element.on('tab(per)', function (data) {
        if (data.index == 0) {
            xtreePer($(".role-item.active").attr("data-id"));
        }
    });

    function xtreePer(id) {
        $.post("/permission/menu/xtree/" + id, function (res) {
            var json = res.data;
            permissionXtree = new xtree({
                elem: 'permissionXtree', //(必填) 放置xtree的容器
                form: form, //(必填) layui 的 from
                data: json,  //(必填) json数据
                icon: {
                    end: "" //末尾节点的图标
                }
            });
        });
    }

    //获得所有选中的checkbox
    function getAllChecked() {
        var permissionStr = "";
        var oCks = permissionXtree.GetAllCheckBox(); //获取全部checkbox
        for (var i = 0; i < oCks.length; i++) {
            if (oCks[i].checked) {
                permissionStr += "," + oCks[i].value;
            }
        }
        if (permissionStr !== "") {
            permissionStr += ",";
        }
        return permissionStr;
    }

    xtreePer($(".role-item.active").attr("data-id"));

    form.on("submit(menuPer)", function (data) {
        var str = getAllChecked();
        data.field.roleId = $(".role-item.active").attr("data-id");
        data.field.permissionIds = str.substring(1, str.length);
        //弹出loading
        var index = top.layer.msg('数据保存中，请稍候...', {icon: 16, time: false, shade: 0.8});
        $.post("/role/menuPer/save", data.field, function (res) {
            if (res.code == 200) {
                top.layer.close(index);
                layer.msg("保存成功！");
                xtreePer($(".role-item.active").attr("data-id"));
            } else {
                top.layer.close(index);
                layer.msg(res.msg);
            }
        });
        top.layer.close(index);
        return false;
    });

    $(".add_btn").click(function () {
        layui.layer.open({
            title: "添加角色",
            type: 2,
            area: ["350px", "245px"],
            content: "/role/add"
        })
    });

    $(".refresh").click(function () {
        location.reload();
    });

});